# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import asyncio
import os

from camel.models import ModelFactory
from camel.prompts import TextPrompt
from camel.types import ModelPlatformType, ModelType

import oasis
from oasis import ActionType, AgentGraph, LLMAction, SocialAgent, UserInfo


async def main():
    # Define the model for the agents
    openai_model = ModelFactory.create(
        model_platform=ModelPlatformType.OPENAI,
        model_type=ModelType.GPT_4O_MINI,
    )

    # Define the available actions for the agents
    available_actions = [
        ActionType.LIKE_POST, ActionType.DISLIKE_POST, ActionType.CREATE_POST,
        ActionType.CREATE_COMMENT, ActionType.LIKE_COMMENT,
        ActionType.DISLIKE_COMMENT, ActionType.FOLLOW, ActionType.MUTE,
        ActionType.PURCHASE_PRODUCT
    ]

    seller_template = TextPrompt('Your aim is: {aim} Your task is: {task}')

    profile = {
        "aim": "Persuade people to buy `GlowPod` lamp.",
        "task": "Using roleplay to tell some story about the product.",
    }

    agent_graph = AgentGraph()
    agent_1 = SocialAgent(
        agent_id=0,
        user_info=UserInfo(
            user_name="snackslut",
            name="Snack Slut",
            description="I taste so you don’t have to.",
            profile=profile,
        ),
        user_info_template=seller_template,
        agent_graph=agent_graph,
        model=openai_model,
        available_actions=available_actions,
    )
    agent_graph.add_agent(agent_1)

    agent_2 = SocialAgent(
        agent_id=1,
        user_info=UserInfo(
            user_name="bubble",
            name="Bob",
            description="A boy",
            profile=None,
            recsys_type="reddit",
        ),
        agent_graph=agent_graph,
        model=openai_model,
        available_actions=available_actions,
    )
    agent_graph.add_agent(agent_2)

    # Define the path to the database
    db_path = "./data/reddit_simulation.db"
    os.environ["OASIS_DB_PATH"] = os.path.abspath(db_path)

    # Delete the old database
    if os.path.exists(db_path):
        os.remove(db_path)

    # Make the environment
    env = oasis.make(
        agent_graph=agent_graph,
        platform=oasis.DefaultPlatformType.REDDIT,
        database_path=db_path,
    )

    # Run the environment
    await env.reset()

    # Sign up the profuct
    await env.platform.sign_up_product(product_id=1, product_name="GlowPod")

    for _ in range(5):
        actions = {
            agent: LLMAction()
            for _, agent in env.agent_graph.get_agents()
        }
        await env.step(actions)

    # Close the environment
    await env.close()


if __name__ == "__main__":
    asyncio.run(main())
